package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Medicinal;
import com.ruoyi.system.service.IMedicinalService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;


@RestController
@RequestMapping("/system/medicinal")
public class MedicinalController extends BaseController
{
    @Autowired
    private IMedicinalService medicinalService;


    @PreAuthorize("@ss.hasPermi('system:medicinal:list')")
    @GetMapping("/list")
    public TableDataInfo list(Medicinal medicinal)
    {
        startPage();
        List<Medicinal> list = medicinalService.selectMedicinalList(medicinal);
        return getDataTable(list);
    }


    @PreAuthorize("@ss.hasPermi('system:medicinal:export')")
    @Log(title = "药品信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Medicinal medicinal)
    {
        List<Medicinal> list = medicinalService.selectMedicinalList(medicinal);
        ExcelUtil<Medicinal> util = new ExcelUtil<Medicinal>(Medicinal.class);
        util.exportExcel(response, list, "药品信息管理数据");
    }


    @PreAuthorize("@ss.hasPermi('system:medicinal:query')")
    @GetMapping(value = "/{drugid}")
    public AjaxResult getInfo(@PathVariable("drugid") Long drugid)
    {
        return AjaxResult.success(medicinalService.selectMedicinalByDrugid(drugid));
    }


    @PreAuthorize("@ss.hasPermi('system:medicinal:add')")
    @Log(title = "药品信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Medicinal medicinal)
    {
        return toAjax(medicinalService.insertMedicinal(medicinal));
    }


    @PreAuthorize("@ss.hasPermi('system:medicinal:edit')")
    @Log(title = "药品信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Medicinal medicinal)
    {
        return toAjax(medicinalService.updateMedicinal(medicinal));
    }


    @PreAuthorize("@ss.hasPermi('system:medicinal:remove')")
    @Log(title = "药品信息管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{drugids}")
    public AjaxResult remove(@PathVariable Long[] drugids)
    {
        return toAjax(medicinalService.deleteMedicinalByDrugids(drugids));
    }
}
